package com.android.settings.audio.peq.peq.view.horizontalscrollview;


import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.android.settings.R;


public class CenterLockHorizontalScrollviewWithTabBg extends HorizontalScrollView {
    Context context;
    private static final int MAX_SCROLL_HEIGHT = 280;
    int prevIndex = 0;
    private boolean handleStop = false;
    private LinearLayout mChildView;

    public CenterLockHorizontalScrollviewWithTabBg(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.setSmoothScrollingEnabled(true);
    }

    public void setCenter(int index, View selectView) {

        ViewGroup parent = (ViewGroup) getChildAt(0);

        ViewGroup pView = (ViewGroup) parent.getChildAt(0);

        View preView = pView.getChildAt(prevIndex);

        preView.setBackgroundResource(R.color.peq_transparent);

        View view = parent.getChildAt(index);

        int screenWidth = ((Activity) context).getWindowManager()
                .getDefaultDisplay().getWidth();
        if (view == null)
            return;
        int scrollX = (view.getLeft() - (screenWidth / 2))
                + (view.getWidth() / 2);

        this.smoothScrollTo(scrollX, 0);

        prevIndex = index;
    }

    public void setCenter(View view) {
        int result = (int) view.getX() - dip2px(getContext(), 160);
        if (result < 0)
            result = 0;
        this.smoothScrollTo(result, 0);

    }

    @Override
    protected void onFinishInflate() {
        // get child View
        if (getChildCount() > 0) {
            this.mChildView = (LinearLayout) getChildAt(0);
        }
        super.onFinishInflate();
    }

    private float touchX = 0;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (arg0.getAction() == MotionEvent.ACTION_DOWN) {
            // get touch X
            touchX = arg0.getX();
        }
        return super.onInterceptTouchEvent(arg0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mChildView != null) {
            commonOnTouchEvent(ev);
        }
        return super.onTouchEvent(ev);
    }

    // Scroll drag
    private static final float SCROLL_DRAG = 0.4f;

    private void commonOnTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_UP:
                if (mChildView.getScrollX() != 0) {
                    handleStop = true;
                    startAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float nowX = ev.getX();
                int deltaX = (int) (touchX - nowX);
                touchX = nowX;
                if (isEdge()) {
                    int offset = mChildView.getScrollX();
                    if (offset < MAX_SCROLL_HEIGHT && offset > -MAX_SCROLL_HEIGHT) {
                        mChildView.scrollBy((int) (deltaX * SCROLL_DRAG), 0);
                        handleStop = false;
                    }
                }
                break;
            default:
                break;
        }
    }

    /*
     * whether to edge
     */
    private boolean isEdge() {
        // get the child view Width
        int childViewWidth = mChildView.getMeasuredWidth();
        // get the ScrollView Width
        int srollViewWidth = this.getWidth();
        // get
        int tempOffset = childViewWidth - srollViewWidth;
        int scrollX = this.getScrollX();
        if (scrollX == 0 || scrollX == tempOffset) {
            return true;
        }
        return false;
    }

    private float RESET_RADIO = 0.9f;

    private void startAnimation() {
        resetChildViewPositionHandler.sendEmptyMessage(0);
    }

    private float childScrollX = 0;
    Handler resetChildViewPositionHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            childScrollX = mChildView.getScrollX();
            if (childScrollX != 0 && handleStop) {
                childScrollX = childScrollX * RESET_RADIO;
                if (Math.abs(childScrollX) <= 2) {
                    childScrollX = 0;
                }
                mChildView.scrollTo((int) childScrollX, 0);
                this.sendEmptyMessageDelayed(0, 3);
            }
        }

        ;
    };

    public void scoolToEnd() {
        int childViewWidth = mChildView.getMeasuredWidth();
        // get the ScrollView Width
        int srollViewWidth = this.getWidth();
        // get
        int tempOffset = childViewWidth - srollViewWidth;
        scrollTo(tempOffset, 0);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public void setSelectedItemView(View itemView) {
        //LogPlus.d("itemView:" + itemView);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
